package com.github.rxyor.netty.exemple.case2;

import static com.github.rxyor.netty.exemple.Config.Server.PORT;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 *<p>
 *
 *</p>
 *
 * @author liuyang
 * @date 2020/5/23 周六 16:03:00
 * @since 1.0.0
 */
public class ChatServer {

    public static void main(String[] args) throws Exception {
        ChatServer server = new ChatServer();
        server.startup();
    }

    private void startup() throws Exception {
        final EventLoopGroup boss = new NioEventLoopGroup();
        final EventLoopGroup work = new NioEventLoopGroup();

        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(boss, work)
                .channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChatServerInitializer());

            ChannelFuture future = bootstrap.bind(PORT).sync();
            future.channel().closeFuture().sync();
        } finally {
            boss.shutdownGracefully();
            work.shutdownGracefully();
        }

    }
}
